package cn.xc.org.controller;

import cn.xc.basic.util.AjaxResult;
import cn.xc.basic.util.PageList;
import cn.xc.org.domain.Department;
import cn.xc.org.query.DepartmentQuery;
import cn.xc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentSeriver;

    @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id){
        return departmentSeriver.loadById(id);
    }

    @GetMapping
    public List<Department> loadAll(){
        return departmentSeriver.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentSeriver.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentSeriver.save(department);
            }else {
                departmentSeriver.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
            return departmentSeriver.queryPage(departmentQuery);
    }

    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            departmentSeriver.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentSeriver.deptTree();
    }



}
